<template>
  <a-modal
    title="产品类型详情"
    width="80%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
    :footer="null"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="编码"
              :labelCol="labelCol_T"
              :wrapperCol="wrapperCol_T"
            >
              <a-input readOnly placeholder="请输入编码" v-decorator="['code', {rules: [{required: true, message: '请输入编码！'}]}]" />
            </a-form-item>
            <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="名称"
              :labelCol="labelCol_T"
              :wrapperCol="wrapperCol_T"
            >
              <a-input readOnly placeholder="请输入名称" v-decorator="['name', {rules: [{required: true, message: '请输入名称！'}]}]" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="父级"
              :labelCol="labelCol_T"
              :wrapperCol="wrapperCol_T"
            >
              <a-tree-select
                :open="false"
                v-decorator="['pid', {initialValue: 'pid',rules: [{ required: true, message: '请选择父级！' }]}]"
                style="width: 100%"
                :dropdownStyle="{ maxHeight: '300px', overflow: 'auto' }"
                :treeData="proTree"
                placeholder="请选择父级"
              >
                <span slot="title" slot-scope="{ id }">{{ id }}
                </span>
              </a-tree-select>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              :labelCol="labelCol_T"
              :wrapperCol="wrapperCol_T"
              label="排序"
            >
              <a-input
                readOnly
                placeholder="请输入排序"
                style="width: 100%"
                v-decorator="['sort', { initialValue: 'sort' }]"/>
            </a-form-item>
          </a-col>
        </a-row>
        <json-add
          ref="jsonAdd"
          :fieldProp="fieldProp"
        />
        <a-form-item
          label="备注"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-textarea readOnly placeholder="请输入备注" v-decorator="['remarks']" :auto-size="{minRows:3,maxRows:6}"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { getProTree } from '@/api/modular/main/protype/proTypeManage'
import { fieldConfigList } from '@/api/modular/main/fieldconfig/fieldConfigManage'
import JsonAdd from '@/components/MyComp/JsonAdd'
export default {
  components: {
    JsonAdd
  },
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 20 }
      },
      labelCol_T: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol_T: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      proTree: [],
      visible: false,
      confirmLoading: false,
      fieldProp: {},
      form: this.$form.createForm(this)
    }
  },
  created() {
    if (this.hasPerm('fieldConfig:list')) {
      fieldConfigList({ tableName: 'dw_pro_type' }).then(res => {
        res.data.forEach(item => {
          // 查询自定义字段
          this.fieldProp[item.fieldIndex] = {
            type: item.fieldType,
            title: item.fieldTitle,
            'x-decorator': 'FormItem',
            'x-component': 'a-input',
            'x-component-props': {
              placeholder: '请输入',
              readOnly: true,
              style: {
                width: '100%'
              }
            }
          }
        })
      })
    }
  },
  methods: {
    /**
     * 获取类型树，并加载于表单中
     */
    getProTree () {
      getProTree().then((res) => {
        this.formLoading = false
        if (!res.success) {
          this.proTree = []
          return
        }
        this.proTree = [{
          'id': '-1',
          'parentId': '0',
          'title': '顶级',
          'value': '0',
          'pid': '0',
          'children': res.data
        }]
      })
    },

    // 初始化方法
    detail (record) {
      this.visible = true
      this.getProTree()
      setTimeout(() => {
        this.form.setFieldsValue(
          {
            code: record.code,
            id: record.id,
            name: record.name,
            pid: record.pid,
            remarks: record.remarks,
            sort: record.sort
          }
        )
        this.$refs.jsonAdd.getAddForm().setValues(JSON.parse(record.josn))
      }, 100)
    },
    handleCancel () {
      this.$refs.jsonAdd.getAddForm().reset()
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
